<!DOCTYPE html>
<html lang="zh-CN">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>内存修改工具</title>
	<style>
		body {
			font-family: Arial, sans-serif;
			margin: 20px;
		}

		form {
			margin-bottom: 20px;
		}

		label {
			display: block;
			margin-bottom: 5px;
		}

		input,
		select {
			margin-bottom: 10px;
			padding: 5px;
		}

		button {
			padding: 5px 10px;
			cursor: pointer;
		}

		#results {
			margin-top: 20px;
		}

		.pagination {
			margin-top: 10px;
		}
	</style>
</head>

<body>
	<h1>内存修改工具</h1>
	<form id="processForm">
		<label for="pid">选择进程:</label>
		<select id="pid" name="pid"></select>
		<br>
		<label for="value">输入要查询的值:</label>
		<input type="number" id="value" name="value" required>
		<br>
		<button type="submit">搜索内存</button>
        <button type="button" id="resetButton">重置查询结果</button>
	</form>
	<div id="results"></div>

	<script>
		// 获取进程列表
		fetch('/processes')
			.then(response => response.json())
			.then(processes => {
				const pidSelect = document.getElementById('pid');
				for (const [exeName, pid] of Object.entries(processes)) {
					const option = document.createElement('option');
					option.value = pid;
					option.textContent = `${exeName} (PID: ${pid})`;
					pidSelect.appendChild(option);
				}
			})
			.catch(error => console.error('获取进程列表失败:', error));

		let currentPage = 1;
		let currentPid = null;

		// 处理搜索表单提交
		document.getElementById('processForm').addEventListener('submit', function (e) {
			e.preventDefault();
			currentPid = document.getElementById('pid').value;
			const value = document.getElementById('value').value;
			currentPage = 1;
			searchMemory(currentPid, value, currentPage);
		});

        // 处理重置按钮点击事件
		document.getElementById('resetButton').addEventListener('click', function () {
			fetch('/reset', {
				method: 'GET'
			})
			.then(response => {
				if (response.ok) {
					const resultsDiv = document.getElementById('results');
					resultsDiv.innerHTML = '<p>查询结果已重置</p>';
				} else {
					throw new Error('重置失败');
				}
			})
			.catch(error => {
				const resultsDiv = document.getElementById('results');
				resultsDiv.innerHTML = `<p>重置失败: ${error.message}</p>`;
			});
		});

		function searchMemory(pid, value, page) {
			fetch(`/search?pid=${pid}&value=${value}&page=${page}`)
				.then(response => response.json())
				.then(data => {
					const resultsDiv = document.getElementById('results');
					resultsDiv.innerHTML = '';

					if (data.addresses.length === 0) {
						resultsDiv.innerHTML = '<p>未找到匹配的值</p>';
					} else {
						resultsDiv.innerHTML = `<p>找到匹配的值，共 ${data.total} 条结果，当前显示第 ${(page - 1) * 10 + 1} 到 ${Math.min(page * 10, data.total)} 条:</p>`;
						const ul = document.createElement('ul');
						data.addresses.forEach(addr => {
							const li = document.createElement('li');
							li.textContent = `0x${addr.toString(16).toUpperCase()}`;

							const input = document.createElement('input');
							input.type = 'number';
							input.placeholder = '新值';

							const button = document.createElement('button');
							button.textContent = '修改';
							button.addEventListener('click', function () {
								const newValue = input.value;
								fetch(`/write?pid=${pid}&addr=${addr}&newValue=${newValue}`)
									.then(response => {
										if (response.ok) {
											alert('内存值修改成功');
										} else {
											response.text().then(error => alert(`修改失败: ${error}`));
										}
									})
									.catch(error => alert(`修改失败: ${error}`));
							});

							li.appendChild(input);
							li.appendChild(button);
							ul.appendChild(li);
						});
						resultsDiv.appendChild(ul);

						// 添加分页按钮
						const paginationDiv = document.createElement('div');
						paginationDiv.className = 'pagination';
						if (page > 1) {
							const prevButton = document.createElement('button');
							prevButton.textContent = '上一页';
							prevButton.addEventListener('click', function () {
								searchMemory(currentPid, document.getElementById('value').value, page - 1);
							});
							paginationDiv.appendChild(prevButton);
						}
						if (page * 10 < data.Total) {
							const nextButton = document.createElement('button');
							nextButton.textContent = '下一页';
							nextButton.addEventListener('click', function () {
								searchMemory(currentPid, document.getElementById('value').value, page + 1);
							});
							paginationDiv.appendChild(nextButton);
						}
						resultsDiv.appendChild(paginationDiv);
					}
				})
				.catch(error => {
					const resultsDiv = document.getElementById('results');
					resultsDiv.innerHTML = `<p>搜索失败: ${error}</p>`;
				});
		}
	</script>
</body>

</html>